﻿using System;

namespace Domain
{
    internal class Angle
    {
        public Point Corner { get; private set; }
        public Point First  { get; private set; }
        public Point Second { get; private set; }

        public Angle(Point first, Point corner, Point second)
        {
            Corner = corner;
            First  = first;
            Second = second;
        }
        public double GetRadian()
        {
            var c = GetLength(First, Corner);
            var a = GetLength(Corner, Second);
            var b = GetLength(Second, First);

            if (IsAngle())
            {
                var cos = (c*c + a*a - b*b) / (2*a*c);
                return Math.Acos(cos);
            }
            throw new BadAngleException();
        }
        private static double GetLength(Point A, Point B)
        {
            var AB = new Edge(A, B);
            return AB.GetLength();
        }
        private bool IsAngle()
        {
            var c = GetLength(First, Corner);
            var a = GetLength(Corner, Second);
            var cIsNonzero = !DoubleComparator.AreEqual(c, 0.0);
            var aIsNonzero = !DoubleComparator.AreEqual(a, 0.0);

            return cIsNonzero && aIsNonzero;
        }
    }
}
